#!/usr/bin/env python
# coding: utf-8

# ## 本代码有如下练习：
# >- 1、[地理编码](https://lbs.amap.com/api/webservice/guide/api/georegeo)
# >> + 1.1 地理编码  
# >> + 1.2 逆地理编码  
# >- 2、[路径规划](https://lbs.amap.com/api/webservice/guide/api/direction)  
# >> + 2.1 步行路径规划  
# >> + 2.2 巴士路径规划  
# >> + 2.3 驾车路径规划  
# >> + 2.4 骑行路径规划  
# >- 3、[行政区域查询](https://lbs.amap.com/api/webservice/guide/api/district)  
# >- 4、[搜索POI](https://lbs.amap.com/api/webservice/guide/api/search)  
# >- 5、[IP定位](https://lbs.amap.com/api/webservice/guide/api/ipconfig)  
# >- 6、[批量请求接口](https://lbs.amap.com/api/webservice/guide/api/batchrequest)  
# >- 7、[静态地图](https://lbs.amap.com/api/webservice/guide/api/staticmaps)  
# >- 8、[坐标转换](https://lbs.amap.com/api/webservice/guide/api/convert)  
# >- 9、[天气查询](https://lbs.amap.com/api/webservice/guide/api/weatherinfo)  
# >- 10、[输入提示](https://lbs.amap.com/api/webservice/guide/api/inputtips)  
# >- 11、[交通态势](https://lbs.amap.com/api/webservice/guide/api/trafficstatus)  

# In[3]:


# 准备工作
import requests, json
import pandas as pd
from PIL import Image
from io import BytesIO
key ="a9758bee89912716bcdb624bc0338c84"


# ### 1、地理编码  
# > 1.1、[地理编码](https://lbs.amap.com/api/webservice/guide/api/georegeo)
# >> 1. 结构化地址含义：首先，地址肯定是一串字符，内含国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦等建筑物名称。按照由大区域名称到小区域名称组合在一起的字符。一个有效的地址应该是独一无二的。注意：针对大陆、港、澳地区的地理编码转换时可以将国家信息选择性的忽略，但省、市、城镇等级别的地址构成是不能忽略的。暂时不支持返回台湾省的详细地址信息。  
# >> 2. url = "https://restapi.amap.com/v3/geocode/geo?parameters"   
# >> 3. requests.get  
# >> 4. 参数：key, address  



def geocode(key,address,city=None,batch="false",sig=None,output="json",callback=None):
    """ 高德api地理编码获取"""
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params_geocode = {
        "key" : key,
        "address" : address,
        "city": city,
        "batch": batch,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    
    response = requests.get(url=url, params=params_geocode)
    results = response.json()
    return results



# > 1.2、[逆地理编码](https://lbs.amap.com/api/webservice/guide/api/georegeo)  
# >> 1. url = "https://restapi.amap.com/v3/geocode/regeo?parameters"  
# >> 2. requests.get  
# >> 3. 参数：key, location  




def regeocode(key,location,poitype=None,radius=1000,extensions="base",batch="false",roadlevel=1,sig=None,output="json",callback=None,homeorcorp=0):
    """ 高德api逆地理编码获取 """
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,              
        "poitype": poitype,
        "radius": radius,
        "extensions": extensions,
        "batch": batch,
        "roadlevel":roadlevel,
        "sig": sig,
        "output": output,
        "callback": callback,
        "homeorcorp": homeorcorp

    }
    response = requests.get(url,params=params)
    results = response.json()
    return results




# ### 2、路径规划 
# > 2.1、[路径规划 - 步行](https://lbs.amap.com/api/webservice/guide/api/direction)
# >> 1. url = "https://restapi.amap.com/v3/direction/walking?parameters"    
# >> 2. requests.get    
# >> 3. 参数：key, origin, destination    




# 2.1、步行
def walk(key,origin,destination,sig=None,output="json",callback=None):
    """ 高德api路径规划步行路径获取 """
    url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,        
        "sig": sig,
        "output": output,
        "callback": callback
    }
    response = requests.get(url,params=params)
    results = response.json()
    return results




# > 2.2、[路径规划 - 巴士](https://lbs.amap.com/api/webservice/guide/api/direction)
# >> 1. url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"    
# >> 2. requests.get    
# >> 3. 参数：key, origin, destination,city   

# In[17]:


# 2.2、搭巴士
def bus(key,origin,destination,city,cityd=None,extensions="base",strategy=0,nightflag=0,date=None,time=None,sig=None,output="json",callback=None):
    """ 高德api路径规划巴士路径获取 """
    url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "city": city,
        "cityd": cityd,
        "extensions": extensions,
        "strategy": strategy,
        "nightflag": nightflag,
        "date": date,
        "time": time,
        "sig": sig,
        "output": output,
        "callback": callback

    }

    response = requests.get(url,params=params)
    results = response.json()
    return results




# > 2.3、[路径规划 - 驾车](https://lbs.amap.com/api/webservice/guide/api/direction)
# >> 1. url = "https://restapi.amap.com/v3/direction/driving?parameters"    
# >> 2. requests.get    
# >> 3. 参数：key, origin, destination   

# In[42]:


# 2.3、驾车 
def car(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=0,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation="false",nosteps=0,sig=None,output="json",callback=None,extensions="base"):
    """ 高德api路径规划驾车路径获取"""
    url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,        
        "originid": originid,
        "destinationid": destinationid,
        "origintype": origintype,
        "destinationtype": destinationtype,
        "strategy": strategy,
        "waypoints": waypoints,
        "avoidpolygons": avoidpolygons,
        "avoidroad": avoidroad,
        "province": province,
        "number": number,
        "cartype": cartype,
        "ferry": ferry,
        "roadaggregation": roadaggregation,
        "nosteps": nosteps,
        "sig": sig,
        "output": output,
        "callback": callback,
        "extensions": extensions

    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results



# > 2.4、[路径规划 - 骑行](https://lbs.amap.com/api/webservice/guide/api/direction)
# >> 1. url = "https://restapi.amap.com/v4/direction/bicycling?parameters"    
# >> 2. requests.get    
# >> 3. 参数：key, origin, destination   

# In[45]:


# 4、骑行路径
def bicycle(key,origin,destination):
    """ 高德api路径规划骑行路径获取 """
    url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results




# ### 3、行政区域查询 
# > 3、[行政区域查询](https://lbs.amap.com/api/webservice/guide/api/district)
# >> 3.1 url = "https://restapi.amap.com/v3/config/district?parameters"    
# >> 3.2 requests.get    
# >> 3.3 参数：key   

# In[9]:


#
def district(key,keywords,subdistric,page=1,offset=20,extensions="base",district_filter="adcode",callback=None,output="json"):
    """ 高德api行政区域查询 """
    url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key":key,        
        "keywords": keywords,
        "subdistric": subdistric,
        "page": page,
        "offset": offset,
        "extensions": extensions,
        "filter": district_filter,
        "callback": callback,
        "output": output
    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results




# ### 4、搜索POI
# > 4、[搜索POI](https://lbs.amap.com/api/webservice/guide/api/search)  
# >> 4.1 url = "https://restapi.amap.com/v3/place/text?parameters"  
# >> 4.2 requests.get  
# >> 4.3 参数：key, keywords, types  

# In[11]:

# 关键字查询
def key_poi(key,keywords,types=None,city=None,citylimit="false",children=0,offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """ 高德api搜索POI调用 """
    url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "types":types,        
        "city": city,
        "citylimit": citylimit,
        "children": children,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback

    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results


# 周边查询  
def around_poi(key,location,keywords=None,types="050000|070000|120000",city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索"""
    url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key": key,
        "location": location,
        "keywords": keywords,
        "types": types,
        "city": city,
        "radius": radius,
        "sortrule": sortrule,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(url,params=params)
    return r.json()
    

# 多边形查询  
def polygon_poi(key,polygon,keywords=None,types="120000|150000",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """在多边形区域内进行搜索POI"""
    url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
        "key": key,
        "polygon": polygon,
        "keywords": keywords,
        "types": types,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(url, params)
    return r.json()



# ### 5、IP定位
# > 5、[IP定位](https://lbs.amap.com/api/webservice/guide/api/ipconfig)  
# >> 5.1 url = "https://restapi.amap.com/v3/ip?parameters"  
# >> 5.2 requests.get  
# >> 5.3 参数：key 

# In[60]:


def ip(key,ip,sig=None,output="json"):
    """ 高德api的ip定位功能调用 """
    url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":key,       
        "ip": ip,
        "sig": sig,
        "output": output
    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results



# ### 6、[批量请求接口](https://lbs.amap.com/api/webservice/guide/api/batchrequest)  
# > 6.1 url = "https://restapi.amap.com/v3/batch?key=<用户的key> "  
# > 6.2 requests.get  
# > 6.3 参数：key 

# In[ ]:


# 没得用了，不写了


# ### 7、[静态地图](https://lbs.amap.com/api/webservice/guide/api/staticmaps)   
# > 7.1 url = "https://restapi.amap.com/v3/staticmap?parameters"  
# > 7.2 requests.get  
# > 7.3 参数：key, location, zoom 

# In[19]:


def map(key,location,zoom=15,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """静态地图服务通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。"""
    from PIL import Image
    from io import BytesIO
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": key,
        "location": location,
        "zoom": zoom,
        "size": size,
        "scale": scale,
        "markers": markers,
        "labels": labels,
        "paths": paths,
        "traffic": traffic,
        "sig": sig
    }
    r = requests.get(url, params)
    data = Image.open(BytesIO(r.content))
    return data



# ### 8、[坐标转换](https://lbs.amap.com/api/webservice/guide/api/convert)   
# > 8.1 url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"  
# > 8.2 requests.get  
# > 8.3 参数：key, locations 

# In[22]:


def convert(key,locations,coordsys="autonavi",sig=None,output="json"):
    """高德api坐标转换,将其它非高德坐标（如：GPS坐标、mapbar坐标、baidu坐标）转换为高德坐标"""
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params={
        "key":key,
        "locations":locations,
        "coordsys": coordsys,
        "sig": sig,
        "output": output
    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results




# ### 9、[天气查询](https://lbs.amap.com/api/webservice/guide/api/weatherinfo)   
# > 9.1 url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"  
# > 9.2 requests.get  
# > 9.3 参数：key, city

# In[25]:


def weather(key,city,extension="all",output="json"):
    """高德api天气查询"""
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city,
        "extensions":extension,
        "output":output
    }
    
    response = requests.get(url,params=params)
    results = response.json()
    return results



# ### 10、[输入提示](https://lbs.amap.com/api/webservice/guide/api/inputtips)   
# > 10.1 url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"  
# > 10.2 requests.get  
# > 10.3 参数：key, keywords

# In[34]:


def hint(key,keywords,city,citylimit):
    """高德api输入提示"""
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "city":city,
        "citylimit":citylimit
    }
        
    response = requests.get(url,params=params)
    results = response.json()
    return results




# ### 11、[交通态势](https://lbs.amap.com/api/webservice/guide/api/trafficstatus)   
# > 11.1 url = "https://restapi.amap.com/v3/traffic/status/rectangle?parameters"  
# > 11.2 requests.get  
# > 11.3 参数：key, level

# In[38]:


def traffic(key,level):
    """高德api交通态势"""
    url = "https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
    params = {
        "key":key,
        "level":level
    }
        
    response = requests.get(url,params=params)
    results = response.json()
    return results






